package com.kuqi.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.kuqi.R;
import com.kuqi.ViewComponent.BookCase.IReadTextView;
import com.kuqi.entitiy.Book;
import com.kuqi.entitiy.Chapter;
import com.kuqi.utils.StringHelper;
import java.util.ArrayList;

// 阅读内容的事件适配类
public class ReadContentAdapter extends RecyclerView.Adapter<ReadContentAdapter.ViewHolder> {

    // 页面内容
    private LayoutInflater mInflater;
    // 资源id
    private int mResourceId;
    // 上下文
    private Context mContext;
    // 显示内容的主体
    private RecyclerView rvContent;

    // 当前书本
    private Book mBook;
    // 章节正文数据
    private ArrayList<Chapter> chapters;

    // 单击事件
    private View.OnClickListener mOnClickItemListener;
    // 触摸事件
    private View.OnTouchListener mOnTouchListener;
    // 没有下载到内容，点击刷新事件
    private View.OnClickListener mOnReLoadListener;


    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ViewHolder viewHolder = (ViewHolder) msg.obj;
                    viewHolder.tvErrorTips.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };


    /**
     * 构造函数
     * @param context 上下文
     * @param resourceId 绑定的显示界面
     * @param data 章节内容
     * @param book 书籍信息
     */
    public ReadContentAdapter(Context context, int resourceId, ArrayList<Chapter> data, Book book) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mResourceId = resourceId;
        chapters = data;
        mBook = book;
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        ViewHolder(View arg0) {
            super(arg0);
        }
        IReadTextView tvTitle; // 章节标题
        IReadTextView tvContent; // 正文内容
        TextView tvErrorTips; // 失败的提示信息
    }


    @Override
    public int getItemCount() {
        return chapters.size();
    }

    public Chapter getItem(int position) {
        return chapters.get(position);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        if (rvContent == null) rvContent = (RecyclerView) viewGroup;
        View view = mInflater.inflate(mResourceId, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.tvTitle = view.findViewById(R.id.tv_title);
        viewHolder.tvContent = view.findViewById(R.id.tv_content);
        viewHolder.tvErrorTips = view.findViewById(R.id.tv_loading_error_tips);
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        final Chapter chapter = getItem(i);
        // 暂时隐藏加载不出的提示信息
        viewHolder.tvErrorTips.setVisibility(View.GONE);

        // 章节标题
        viewHolder.tvTitle.setText("【" + chapter.getChapterName() + "】");

        // 章节内容显示
        if (StringHelper.isEmpty(chapter.getChapterContent())) {
            viewHolder.tvContent.setText("无法获取到本章节内容，请检查网络或者程序数据！");
        } else {
            viewHolder.tvContent.setText(chapter.getChapterContent());
        }

        // 给itemView 设置触摸事件
        if (mOnTouchListener != null){
            viewHolder.itemView.setOnTouchListener(mOnTouchListener);
        }

        // 给itemView 设置点击事件
        if(mOnClickItemListener != null){
            viewHolder.itemView.setOnClickListener(mOnClickItemListener);
        }

        // 给失败重载字条添加点击事件
        if(mOnReLoadListener != null){
            viewHolder.tvErrorTips.setOnClickListener(mOnReLoadListener);
        }
    }

    // 设置点击监听
    public void setOnClickItemListener(View.OnClickListener mOnClickItemListener) {
        this.mOnClickItemListener = mOnClickItemListener;
    }

    // 设置触摸监听
    public void setOnTouchListener(View.OnTouchListener mOnTouchListener) {
        this.mOnTouchListener = mOnTouchListener;
    }

    // 设置重新加载监听
    public void setOnReLoadListener(View.OnClickListener mOnReLoadListener) {
        this.mOnReLoadListener = mOnReLoadListener;
    }


}
